<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/11/27
 * Time: 20:54
 */

namespace App\Services\ProdTempCreationMasksService;


use App\Services\ImgServices\ImgTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Validator;
class ProdTempCreationMasksService
{
    protected $imgTool;

    /**
     * ProdTempCreationMasksService constructor.
     */
    public function __construct(ImgTool $imgTool)
    {
        $this->imgTool=$imgTool;
    }
    public function generateId(){
        return date('YmdHis') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /***
     * 保存创作启动蒙板
     * @param array $data
     * @return bool
     */
    public function saveCreationMask(array $data){
        try{
            $original_image=$data['original_image'];
            $ins_data['name']=$data['name'];
            $ins_data['product_temp_id']=$data['resource_id'];
            $ins_data['product_temp_category_id']=$data['temp_category_id'];
            if(!empty($original_image)){
                $img_path=$this->imgTool->imgSave($original_image,2);
                $ins_data['original_image']=$img_path;
            }
            DB::beginTransaction();
            $ins_data['id']=(isset($data['id'])&&!empty($data['id']))?$data['id']:0;
            $res=self::saveCreationMaskDB($ins_data);
            return $res;
        }catch (\Exception $e){
            DB::rollback();
            Log::info($e);
            return false;
        }finally{
            DB::commit();
        }
    }

    /***
     * 获取列表
     * @param array $data
     * @return mixed
     */
    public function getCreationMaskList(array  $data){
        $per_page=isset($data['per_page'])?$data['per_page']:10;
        $creation_masks=DB::table('creation_masks')->where(array('state'=>1))->paginate($per_page);
        foreach ($creation_masks as $creation_mask){
            $original_image=ImgTool::getImgUrlByImgPath($creation_mask->original_image);
            $creation_mask->original_image=empty($original_image)?'':$original_image;
        }
        return $creation_masks;
    }
    /***
     * 保存 更新数据到数据库中
     * @param array $data
     * @return bool
     */
    private function saveCreationMaskDB(array $data){
        try{
            $id=isset($data['id'])?$data['id']:0;
            $data['state']=1;
            $data['updated_at']=time();
            if(!empty($id)){
                $res=DB::table('creation_masks')->where(array('id'=>$id))->update($data);
            }else{
                $data['created_at']=time();
                $res=DB::table('creation_masks')->insert($data);
            }
            return ($res==1)?true:false;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
    /***
     * 查看名称是不是已使用
     * @param $name
     * @return bool
     */
    public function creationMaskExistByName($name,$id=0){
        $name=trim($name);
        if(!empty($id)){
            $num=  DB::table('creation_masks')->where(array('state'=>1,'name'=>$name))
                ->where('id','<>',$id)->count();
        }else{
            $num=  DB::table('creation_masks')->where(array('state'=>1,'name'=>$name))->count();
        }

        return ($num===0)?false:true;
    }
    public function getCreationMaskById($id){
        $creation_mask=  DB::table('creation_masks')->where(array('state'=>1,'id'=>$id))->first();
        return $creation_mask;
    }

    /****
     * 更新排序
     * @param $id
     * @param $sort
     * @return bool
     */
    public function updateCreationMaskSort($id,$sort){
        if(empty($id)||(!is_int($id))||(!is_int($sort))||($sort<0)){
            return false;
        }
        $update_res=  DB::table('creation_masks')->where(array('state'=>1,'id'=>$id))
            ->update(array(
                'sort'=>$sort,
                'updated_at'=>time(),
            ));
        return ($update_res===1)?true:false;
    }

    /***
     * 删除标签
     * @param $id
     * @return bool
     */
    public function deleteCreationMask($id){
        if(empty($id)||(!is_int($id))){
            return false;
        }
        $update_res=  DB::table('creation_masks')->where(array('state'=>1,'id'=>$id))
            ->update(array(
                'state'=>0,
                'updated_at'=>time(),
            ));
        return ($update_res===1)?true:false;
    }
    /**
     * 数据验证
     * @param Request $request
     * @param $check_data
     * @return mixed
     */
    public static function store(Request $request,$check_data)
    {
        $v = Validator::make($request->all(), $check_data);
        return $v;
    }
}